#region Summary

// 
// Owner : JackieHan 
// 
// Solution : DailyCollection 
// 
// Project : Jackie.Utility.Notify.Services
// 
// File : TitleManager.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 消息标题管理类
// 
// History : 
//			2011-04-28 17:19:01	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using Jackie.Utility.CustomException;
using Jackie.Utility.Notify.Domain;

#endregion

namespace Jackie.Utility.Notify.Services.Manager
{
    /// <summary> 
    /// 消息标题管理类
    /// </summary>
    /// <remarks>
    /// 初始化一个保持连接的消息标题，所有客户端都必须订阅
    /// </remarks>
    public class TitleManager
    {
        #region Singleton

        private static TitleManager instance;
        private static readonly object LockObject = new object();

        public static TitleManager GetInstance()
        {
            if (instance == null)
            {
                lock (LockObject)
                {
                    if (instance == null)
                        instance = new TitleManager();
                }
            }
            return instance;
        }

        #endregion

        /// <summary>
        /// 系统支持的所有消息标题   key:TitleCode
        /// </summary>
        private readonly IDictionary<string, TitleInfo> titles;

        private TitleManager()
        {
            titles = new Dictionary<string, TitleInfo>
                         {
                             {
                                 "KeepAlive",
                                 new TitleInfo
                                     {Code = "KeepAlive", Discription = "保持连接消息", InstantMessage = true, Name = "保持连接"}
                                 }
                         };
        }

        /// <summary>
        /// 获取保持连接消息标题
        /// </summary>
        /// <returns></returns>
        public TitleInfo GetKeepAliveTitle()
        {
            return titles["KeepAlive"];
        }

        /// <summary>
        /// 消息发布客户端或者服务管理后台发布一个消息标题
        /// </summary>
        /// <param name="info"></param>
        public void PublishTitle(TitleInfo info)
        {
            if (!titles.ContainsKey(info.Code))
            {
                titles.Add(info.Code, info);
            }
            else
            {
                throw new CodeExistedException("TitleInfo", info.Code);
            }
        }

        /// <summary>
        /// 消息发布客户端或者服务管理后台取消一个消息标题
        /// </summary>
        /// <param name="titleCode"></param>
        public void CancelTitle(string titleCode)
        {
            if (titles.ContainsKey(titleCode))
            {
                titles.Remove(titleCode);
            }
            else
            {
                throw new CodeNotExistedException("TitleInfo", titleCode);
            }
        }

        /// <summary>
        /// 获取所有的消息标题
        /// </summary>
        /// <returns></returns>
        public IList<TitleInfo> GetAllTitles()
        {
            return titles.Values.Where(item => !item.Code.Equals("KeepAlive")).ToList();
        }

        /// <summary>
        /// 获取某一个消息标题的详细信息
        /// </summary>
        /// <param name="titleCode">消息标题编码</param>
        /// <returns></returns>
        public TitleInfo GetTitle(string titleCode)
        {
            if (titles.ContainsKey(titleCode))
                return titles[titleCode];
            throw new CodeNotExistedException("TitleInfo", titleCode);
        }

        /// <summary>
        /// 判断一个消息标题是否存在
        /// </summary>
        /// <param name="titleCode">消息标题的代码</param>
        /// <returns>存在：true；不存在：false</returns>
        public bool IsTitleExisted(string titleCode)
        {
            return titles.ContainsKey(titleCode);
        }
    }
}